package com.ubtrobot.serialportlib.serial;



import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Description
 *
 * @author jianlin.duan
 * 2020/11/11, Create file
 */
public class SerialManager implements SerialDataListener{

    private volatile SerialPort mPort;

    private SerialWriter mWriter;

    private List<SerialDataListener> mListenerList = new ArrayList<>();

    private static final SerialManager ourInstance = new SerialManager();

    public static SerialManager getInstance() {
        return ourInstance;
    }

    private SerialManager() {
    }

    /**
     * 默认打开"/dev/ttyS1"接口
     * @param listener
     */
    public void openSerialPort(OnOpenSerialPortListener listener){
        openSerialPort("/dev/ttyS1",57600,listener);
    }

    /**
     * 指定接口打开
     * @param portName
     * @param listener
     */
    public void openSerialPort(String portName,int baudrate,OnOpenSerialPortListener listener){
        HiExecutor.INSTANCE.execute(()->{
            if(mPort==null){
                MyLogger.dLog().d("init serial port, open it");
                try {
                    mPort = new SerialPort(new File(portName), baudrate, 0);
                    mWriter = new SerialWriter(mPort.getOutputStream());
                    HiExecutor.INSTANCE.execute(new SerialReader(mPort.getInputStream(),this));
                } catch (Exception e) {
                    e.printStackTrace();
                    listener.onResult(false,e.getMessage());
                    return;
                }
            }else{
                MyLogger.dLog().d("serial prot is already opened");
            }
            listener.onResult(true,"");
        });
    }
    public void closeSerialPort(){
        if(mPort!=null){
            MyLogger.dLog().d("close serial port");
            mPort.closePort();
            mPort=null;
        }else{
            MyLogger.dLog().d("serial port is already closed");
        }
    }

    public void write(String data){
        if(mPort==null){
            MyLogger.dLog().e("serial port is not opened,please check serial port .");
            return;
        }
        if(mPort.getOutputStream()==null){
            MyLogger.dLog().e("serial outputstream is null,please check serial port .");
            return;
        }
//        MyLogger.dLog().d("write="+data);
        mWriter.write(data);
    }

    public void write(byte[] data){
        if(mPort==null){
            MyLogger.dLog().e("serial port is not opened,please check serial port .");
            return;
        }
        if(mPort.getOutputStream()==null){
            MyLogger.dLog().e("serial outputstream is null,please check serial port .");
            return;
        }
//        MyLogger.dLog().d("write="+data);
        mWriter.write(data);
    }

    public synchronized void registerListener(SerialDataListener listener){
        mListenerList.add(listener);
    }

    public synchronized void unRegisterListener(SerialDataListener listener){
        mListenerList.remove(listener);
    }

    @Override
    public void onDataReceived(String data) {
//        MyLogger.dLog().d("read= "+data);
        synchronized (this){
            for (SerialDataListener listener :
                    mListenerList) {
                listener.onDataReceived(data);
            }
        }
    }
}
